/**
 * 行程编辑、新增
 *
 */
define(["app", "js/services/MessageBox", "js/services/MuseService"
    , "js/controllers/trip/tripActivity/TripActivityCommon.js"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取参数
        var id = $stateParams.id || '';

        //获取数据
        $scope.getDetail = function () {
            MuseService.get(baseServices_restfulUrl + "tripActivity/load.json?id=" + id).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.tripActivity = data.data;
                    } else {
                        MessageBox.notify('获取数据失败:' + data.message);
                    }
                });
        };
        if (id) {
            $scope.getDetail();
        }

        //保存
        $scope.saveObj = function () {
            MuseService.postObj(baseServices_restfulUrl + "tripActivity/save.json", $scope.tripActivity).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify('保存成功!');
                        $state.go('tripActivityList');
                    } else {
                        MessageBox.notify('保存失败：' + data.message);
                    }
                });
        };

        // 取消，返回
        $scope.close = function () {
            $state.go("tripActivityList");
        };

        //保存强制修改状态
        $scope.forceChangeSatate = function () {

            var dialog = MessageBox.confirm("你确定需要强制改变这个行程的状态？这样可能会带来无法预料的后果。");
            dialog.result.then(function () {
                var param = {
                    tripActivityId: $scope.tripActivity.id,
                    state: $scope.tripActivity.state
                }

                MuseService.post(baseServices_restfulUrl + "tripActivity/forceChangeSatate.json", param).success(
                    function (data, status, headers, config) {
                        if (data.state == 0) {
                            MessageBox.notify('修改成功!');
                            $scope.getDetail();
                        } else {
                            MessageBox.notify('修改失败：' + data.message);
                        }
                    })
                .error(function () {
                    MessageBox.error('服务器正在开小差zZZ,修改失败');
                });
            });

        };

        //保存强制修改状态
        $scope.cancelTripActivity = function () {

            var dialog = MessageBox.confirm("你确定取消这个行程？这样会连带取消 行程所属的乘客订单");
            dialog.result.then(function () {
                var param = {
                    tripId: $scope.tripActivity.id,
                    reason: '后台管理员取消'
                }

                MuseService.post(baseServices_restfulUrl + "tripActivity/cancel.json", param).success(
                    function (data, status, headers, config) {
                        if (data.state == 0) {
                            MessageBox.notify('取消成功!');
                            $scope.getDetail();
                        } else {
                            MessageBox.notify('取消失败：' + data.message);
                        }
                    })
                .error(function () {
                    MessageBox.error('服务器正在开小差zZZ,修改失败');
                });
            });

        };

        /**
         * 查看 乘客行程列表
         * @param tripId
         */
        $scope.showTripDetail = function (tripId) {
            $state.go("tripEdit", {id: tripId});
        }
    }

    controller.$inject = deps;
    app.lazy.controller("TripActivityEditController", controller);
});
